package org.wyn.service.impl;

import java.util.List;
import java.util.regex.Pattern;

import org.apache.commons.lang.StringUtils;
import org.bson.types.ObjectId;
import org.wyn.mongodb.bean.BecomeFriendRequest;
import org.wyn.mongodb.bean.UserDetail;
import org.wyn.mongodb.dao.impl.BecomeFriendRequestDao;
import org.wyn.mongodb.dao.impl.FreshThingDao;
import org.wyn.mongodb.dao.impl.FriendRelationshipDao;
import org.wyn.mongodb.dao.impl.UserDetailDao;
import org.wyn.service.IFriendService;

import com.google.inject.Inject;

public class FriendServiceImpl implements IFriendService{

	@Override
	public List<UserDetail> searchUserByRealName(String realName) {
		//没输入关键字全查询
		if(StringUtils.isEmpty(realName)){
			return userDetailDao.createQuery().asList();
		}
		Pattern p = Pattern.compile(".*" + realName + ".*");
		return userDetailDao.createQuery().filter("realName", p).asList();
	}
	@Override
	public boolean becomeFriendRequest(String fromUser, String toUser) {
		return becomeFriendRequestDao.createRequest(fromUser, toUser);
	}
	@Override
	public List<BecomeFriendRequest> viewFriendRequestByToUser(String toUser) {
		return becomeFriendRequestDao.viewFriendRequestByToUser(toUser);
	}
	@Override
	public boolean agreeRequest(String toUser, String requestId) {
		BecomeFriendRequest bfr = becomeFriendRequestDao.findOne("requestId", new ObjectId(requestId));
		if(null == bfr)
			return false;//请求不存在
		if(!toUser.equals(bfr.getToUser()))
			return false;//toUser不匹配,做这个检查主要考虑安全方面
		friendRelationshipDao.createRelationship(bfr.getFromUser(), bfr.getToUser());//创建好友关系
		becomeFriendRequestDao.delete(bfr);//创建关系后从表中删除请求
		//保存新鲜事
		freshThingDao.saveThingBecomeFriend(bfr.getFromUser(), bfr.getToUser());
		freshThingDao.saveThingBecomeFriend(bfr.getToUser(), bfr.getFromUser());
		return true;
	}
	@Override
	public boolean rejectRequest(String toUser, String requestId) {
		BecomeFriendRequest bfr = becomeFriendRequestDao.findOne("requestId", new ObjectId(requestId));
		if(null == bfr)
			return false;//请求不存在
		if(!toUser.equals(bfr.getToUser()))
			return false;//toUser不匹配,做这个检查主要考虑安全方面
		becomeFriendRequestDao.delete(bfr);//由于是拒绝请求直接把请求删除了就行
		return true;
	}
	@Override
	public List<UserDetail> viewFriendByUserName(String userName) {
		List<String> friends = friendRelationshipDao.findRelationshipByUser(userName);
		return userDetailDao.createQuery().filter("userName in", friends).asList();
	}
	@Inject
	private UserDetailDao userDetailDao;
	@Inject
	private BecomeFriendRequestDao becomeFriendRequestDao;
	@Inject
	private FriendRelationshipDao friendRelationshipDao;
	@Inject
	private FreshThingDao freshThingDao;
	public FreshThingDao getFreshThingDao() {
		return freshThingDao;
	}
	public void setFreshThingDao(FreshThingDao freshThingDao) {
		this.freshThingDao = freshThingDao;
	}
	public UserDetailDao getUserDetailDao() {
		return userDetailDao;
	}
	public void setUserDetailDao(UserDetailDao userDetailDao) {
		this.userDetailDao = userDetailDao;
	}
	public BecomeFriendRequestDao getBecomeFriendRequestDao() {
		return becomeFriendRequestDao;
	}
	public void setBecomeFriendRequestDao(
			BecomeFriendRequestDao becomeFriendRequestDao) {
		this.becomeFriendRequestDao = becomeFriendRequestDao;
	}
	public FriendRelationshipDao getFriendRelationshipDao() {
		return friendRelationshipDao;
	}
	public void setFriendRelationshipDao(FriendRelationshipDao friendRelationshipDao) {
		this.friendRelationshipDao = friendRelationshipDao;
	}
	
}
